package com.bistu.sim.xwy.news.BusinessMode;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bistu.sim.xwy.news.Http.WeatherHttpUtil;
import com.bistu.sim.xwy.news.R;
import com.bistu.sim.xwy.news.MainActivity;
import com.bistu.sim.xwy.news.Weather.UpdateService;
import com.bistu.sim.xwy.news.Weather.Utility;
import com.bistu.sim.xwy.news.gson.Weather;
import com.bistu.sim.xwy.news.gson.WeatherResults;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BusMainActivity extends AppCompatActivity implements View.OnClickListener {

    private Spinner manageType;
    private Spinner placeType;

    private TextView tvFirstDayDate;
    private TextView tvFirstDayPhe;
    private TextView tvFirstDayHigh;
    private TextView tvFirstDayLow;
    private TextView mycity;
    private TextView temperatureText;
    private TextView caseText;

    private SwipeRefreshLayout refreshLayout;

    private LinearLayout index_bottom_bar_home;
/*    private LinearLayout index_bottom_bar_dynamic_state;
    private LinearLayout index_bottom_bar_integral;
    private LinearLayout index_bottom_bar_post;*/
    private LinearLayout index_bottom_bar_me;

    private ImageView index_bottom_bar_home_image;
/*    private ImageView index_bottom_bar_post_image;
    private ImageView index_bottom_bar_dynamic_state_image;
    private ImageView index_bottom_bar_integral_image;*/
    private ImageView index_bottom_bar_me_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_main);

        initView();
        initEvent();

    }

    public void GetrequestWeather(String cityPinYin){
        String url="https://api.seniverse.com/v3/weather/daily.json?key=SeCotTi-iRxdqM2u0&location=" + cityPinYin + "&language=zh-Hans&unit=c&start=0&days=5";
        final Utility util=new Utility();
        Utility.showProgressDialog(BusMainActivity.this);
        WeatherHttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                assert response.body() != null;
                final String weatherData=response.body().string();
                Log.e("MainActivity","得到的数据"+weatherData);
                final Weather weatherInfo= Utility.handleWeatherRsponse(weatherData);
                if(weatherInfo!=null){
                    SharedPreferences.Editor editor= getSharedPreferences("weather",MODE_PRIVATE).edit();
                    editor.putString("WEATHER", weatherData);
                    editor.putBoolean("isSelected",true);
                    editor.apply();
                }
                Utility.dismiss();
                showWeatherInfo(weatherInfo);
            }
        });
    }
    private void showWeatherInfo(final Weather weatherInfo){
        runOnUiThread(() -> {

            WeatherResults results=weatherInfo.getResults().get(0);
            String cityName=results.getLocation().getName();//城市名称


            //将3天内的天气数据获得后存入二维数组
            String [] [] resultDaily=new String[3][5];
            for(int i=0;i<3;i++){
                for(int j=0;j<4;){
                    resultDaily[i][j]=results.getDaily().get(i).getDate();j++;
                    resultDaily[i][j]=results.getDaily().get(i).getText_day();j++;
                    resultDaily[i][j]=results.getDaily().get(i).getHigh();j++;
                    resultDaily[i][j]=results.getDaily().get(i).getLow();j++;
                }
            }
            mycity.setText(cityName);
            temperatureText.setText(resultDaily[0][2]+"℃");
            caseText.setText(resultDaily[0][1]);

            for(int j=0;j<4;){
                //Log.e("resultDaily",resultDaily[0][j]);
                tvFirstDayDate.setText(resultDaily[0][j]);j++;
                tvFirstDayPhe.setText(resultDaily[0][j]);j++;
                tvFirstDayHigh.setText(resultDaily[0][j]+"℃");j++;
                tvFirstDayLow.setText(resultDaily[0][j]+"℃");j++;
            }


        });
    }

    private void initView() {

        refreshLayout=findViewById(R.id.swipe_refresh);
        temperatureText=findViewById(R.id.temperature_text);
        caseText=findViewById(R.id.case_text);
        mycity=findViewById(R.id.my_city);
        tvFirstDayDate = findViewById(R.id.tv_firstday_date);
        tvFirstDayPhe=findViewById(R.id.tv_firstday_phe);
        tvFirstDayHigh=findViewById(R.id.tv_firstday_high);
        tvFirstDayLow=findViewById(R.id.tv_firstday_low);

        index_bottom_bar_home = findViewById(R.id.index_bottom_bar_home);
/*        index_bottom_bar_dynamic_state = findViewById(R.id.index_bottom_bar_dynamic_state);
        index_bottom_bar_integral=findViewById(R.id.index_bottom_bar_integral);
        index_bottom_bar_post = findViewById(R.id.index_bottom_post);*/
        index_bottom_bar_me=findViewById(R.id.index_bottom_bar_me);

        index_bottom_bar_home_image = (ImageView) findViewById(R.id.index_bottom_bar_home_image);
/*        index_bottom_bar_dynamic_state_image = (ImageView) findViewById(R.id.index_bottom_bar_dynamic_state_image);
        index_bottom_bar_integral_image = (ImageView) findViewById(R.id.index_bottom_bar_integral_image);
        index_bottom_bar_post_image = (ImageView) findViewById(R.id.index_bottom_post_img);*/
        index_bottom_bar_me_image = (ImageView) findViewById(R.id.index_bottom_bar_me_image);
    }

    private void initEvent() {
        index_bottom_bar_home_image.setImageResource(R.mipmap.index_bottom_bar_home_select);

        index_bottom_bar_home.setOnClickListener(this);
/*        index_bottom_bar_dynamic_state.setOnClickListener(this);
        index_bottom_bar_integral.setOnClickListener(this);
        index_bottom_bar_post.setOnClickListener(this);*/
        index_bottom_bar_me.setOnClickListener(this);

        Intent intent=new Intent();
        intent.setClass(BusMainActivity.this, UpdateService.class);
        //startService(intent);
        GetrequestWeather("chengdu");
        refreshLayout.setColorSchemeResources(R.color.primary);
        refreshLayout.setOnRefreshListener(() -> {
            Toast.makeText(BusMainActivity.this,"刷新成功",Toast.LENGTH_SHORT).show();
            //Log.e("MainActivity","返回城市的拼音"+cityPinYin);
            //心知天气的api
            GetrequestWeather("chengdu");
            refreshLayout.setRefreshing(false);
        });
    }

    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.index_bottom_bar_home:
                Intent intent = new Intent(this, BusMainActivity.class);
                BusMainActivity.this.finish();
                startActivity(intent);
                overridePendingTransition(0,0);
                break;
/*            case  R.id.index_bottom_bar_integral:
                Intent intent1 = new Intent(this, MessageActivity.class);
                startActivity(intent1);
                PostActivity.this.finish();
                overridePendingTransition(0,0);
                break;
            case  R.id.index_bottom_bar_dynamic_state:
                Intent intent2 = new Intent(this, FocusActivity.class);
                startActivity(intent2);
                PostActivity.this.finish();
                overridePendingTransition(0,0);
                break;
            case R.id.index_bottom_post:
                Intent intent4 = new Intent(this,PostActivity.class);
                startActivity(intent4);
                PostActivity.this.finish();
                overridePendingTransition(0,0);
                break;*/
            case  R.id.index_bottom_bar_me:
                Intent intent3 = new Intent(this, BusMineActivity.class);
                startActivity(intent3);
                BusMainActivity.this.finish();
                overridePendingTransition(0,0);
                break;
        }
    }

}